﻿using System;
using System.Web.UI;
using System.Reflection;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Collections.Generic;

using ExtraClub.Domain;
using ExtraClub.Domain.Entities;

using SubSonic;
using ExtraClub.Domain.Forms;
using ExtraClub.Domain.Framework;
using System.Web.Security;

namespace ExtraClub.Web.Forms.club {
    public partial class module : BaseForm {
        private VwClubProfile SelectedEntity { get; set; }
        private MembershipUser SelectedClubUser { get; set; }

        protected override void OnLoad(EventArgs e) {
            if (Request.QueryString.Count > 0 && Request.QueryString["id"] != null) {
                SelectedEntity = VwClubProfile.FetchByID(int.Parse(Request.QueryString["id"]));
            }

            if (CurrentCommand == ECommand.New) {
                SelectedEntity = null;
            } else if (CurrentCommand == ECommand.Edit && SelectedEntity == null) {
                SelectedEntity = SiteContext.Current.Club;
            }

            base.OnLoad(e);
        }
        protected override string SetFields() {
            string ret = "null";
            switch (CurrentCommand) {
                case ECommand.New:
                    ret = BuildSaveForm(false);
                    break;
                case ECommand.Edit:
                    ret = BuildSaveForm(true);
                    break;
                case ECommand.Delete:
                    ret = BuildDeletForm();
                    break;
            }
            return ret;
        }

        private string BuildDeletForm() {
            FieldCollection fs = new FieldCollection();
            AddHiddenFieldes(ref fs, SelectedEntity.Id.ToString());

            return fs.JSON();
        }

        //CellularPhone, HomePhone
        private string BuildSaveForm(bool edit) {
            FieldCollection fs = new FieldCollection();

            AddHiddenFieldes(ref fs, SelectedEntity != null ? SelectedEntity.Id.ToString() : "");
            fs.Control = this;
            fs.LabelKeySuffex = "-label";

            #region User Profile
            if (edit) {
                Club club = Club.FetchByID(SelectedEntity.Id);
                SelectedClubUser = Membership.GetUser((Guid)club.Entity.AspnetUserProfileRecords[0].UserId);
            }

            new Field(FieldType.Group, "user-profile", g =>
            {
                g.Legend = UIText["user-profile-legend"];
                g.Fields = new Field[5];

                /* User Name */
                new Field(FieldType.String, AspnetUser.Columns.UserName.ToLower(), f =>
                {
                    f.Size = 30;
                    f.Required = true;
                    f.Value = edit ? SelectedClubUser.UserName : String.Empty;
                    f.Readonly = edit;
                    g.Fields[0] = f;
                }, fs, false);

                /* Email */
                new Field(FieldType.Email, AspnetMembership.Columns.Email.ToLower(), f =>
                {
                    f.Size = 30;
                    f.Required = true;
                    f.Value = edit ? SelectedClubUser.Email : String.Empty;
                    g.Fields[1] = f;
                }, fs, false);

                /* User Name */
                new Field(FieldType.String, AspnetMembership.Columns.Password.ToLower(), f =>
                {
                    f.Size = 30;
                    f.Required = true;
                    f.Value = edit ? SelectedClubUser.GetPassword() : String.Empty;
                    g.Fields[2] = f;
                }, fs, false);


                /* First Name */
                new Field(FieldType.String, AspnetUserProfile.Columns.FirstName.ToLower(), f =>
                {
                    f.Size = AspnetUserProfile.Schema.GetColumn(AspnetUserProfile.Columns.FirstName).MaxLength;
                    f.Required = true;
                    f.Value = edit ? SelectedEntity.FirstName : String.Empty;
                    g.Fields[3] = f;
                }, fs, false);

                /* Family Name */
                new Field(FieldType.String, AspnetUserProfile.Columns.FamilyName.ToLower(), f =>
                {
                    f.Size = AspnetUserProfile.Schema.GetColumn(AspnetUserProfile.Columns.FamilyName).MaxLength;
                    f.Required = true;
                    f.Value = edit ? SelectedEntity.FamilyName : String.Empty;
                    g.Fields[4] = f;
                }, fs, false);

            }, fs);
            #endregion

            #region Club Profile
            new Field(FieldType.Group, "club-profile", g =>
            {
                g.Legend = UIText["club-profile-legend"];
                g.Fields = new Field[10];

                /* Company Name */
                new Field(FieldType.String, Club.Columns.Name.ToLower(), f =>
                {
                    f.Size = Club.Schema.GetColumn(Club.Columns.Name).MaxLength;
                    f.Required = true;
                    f.Value = edit ? SelectedEntity.Name : String.Empty;
                    g.Fields[0] = f;
                }, fs, false);

                /* Club Category */
                new Field(FieldType.Select, Club.Columns.ClubCategoryId.ToLower(), f =>
                {
                    f.Size = Club.Schema.GetColumn(Club.Columns.ClubCategoryId).MaxLength;
                    f.SelectOptions = ClubCategory.GetClubCategories().Options.ToArray();
                    f.SelectValues = ClubCategory.GetClubCategories().Values.ToArray();
                    f.Required = true;
                    f.Value = edit ? SelectedEntity.ClubCategoryId.ToString() : String.Empty;
                    g.Fields[1] = f;
                }, fs, false);

                /* ID Number */
                new Field(FieldType.Number, Club.Columns.IDNumber.ToLower(), f =>
                {
                    f.Size = 9;
                    f.MinLength = 5;
                    f.Required = true;
                    f.Value = edit ? SelectedEntity.IDNumber.ToString() : String.Empty;
                    g.Fields[2] = f;
                }, fs, false);

                /* Address Repository */
                /* Club Email */
                new Field(FieldType.Email, AddressRepository.Columns.Email.ToLower(), f =>
                {
                    f.Size = AddressRepository.Schema.GetColumn(AddressRepository.Columns.Email).MaxLength;
                    f.Required = true;
                    f.Value = edit ? SelectedEntity.Email.ToString() : String.Empty;
                    g.Fields[3] = f;
                }, fs, false);


                /* City */
                new Field(FieldType.Select, AddressRepository.Columns.CityRegion.ToLower(), f =>
                {
                    f.Size = AddressRepository.Schema.GetColumn(AddressRepository.Columns.CityRegion).MaxLength;
                    f.SelectOptions = UISelectOptions[f.CodeName + "-label"].ToArray();
                    f.SelectValues = UISelectValues[f.CodeName + "-label"].ToArray();
                    f.Required = true;
                    f.Value = edit ? SelectedEntity.CityRegion.ToString() : String.Empty;
                    g.Fields[4] = f;
                }, fs, false);

                /* House Number */
                new Field(FieldType.String, AddressRepository.Columns.HouseNumber.ToLower(), f =>
                {
                    f.Size = 4;
                    f.Required = true;
                    f.Value = edit ? SelectedEntity.HouseNumber : String.Empty;
                    g.Fields[5] = f;
                }, fs, false);

                /* Apartment */
                new Field(FieldType.Number, AddressRepository.Columns.Apartment.ToLower(), f =>
                {
                    f.Size = 4;
                    f.Required = true;
                    f.Value = edit ? SelectedEntity.Apartment.ToString() : String.Empty;
                    g.Fields[6] = f;
                }, fs, false);

                /* ZIP */
                new Field(FieldType.Number, AddressRepository.Columns.Zip.ToLower(), f =>
                {
                    f.Size = 5;
                    f.MinLength = 5;
                    f.Required = true;
                    f.Value = edit ? SelectedEntity.Zip.ToString() : String.Empty;
                    g.Fields[7] = f;
                }, fs, false);

                #region Phones
                /* Cellular Phone */
                new Field(FieldType.Combined, AddressRepository.Columns.CellularPhone.ToLower(), f =>
                {
                    f.Required = true;
                    f.Fields = new Field[2];

                    List<string> options = UISelectOptions[f.CodeName + "-label"];
                    /* Prefix */
                    new Field(FieldType.Select, null, a =>
                    {
                        a.SelectOptions = options.ToArray();
                        a.SelectValues = UISelectValues[f.CodeName + "-label"].ToArray();
                        a.Value = edit ? ((int)SelectedEntity.CellularPhonePrefix).ToString("0##") : String.Empty;
                        f.Fields[0] = a;
                    }, null);

                    /* Phone */
                    new Field(FieldType.Number, null, b =>
                    {
                        b.Size = 10;
                        b.Value = edit ? SelectedEntity.CellularPhone.ToString() : String.Empty;
                        f.Fields[1] = b;
                    }, null);

                    g.Fields[8] = f;
                }, fs, false);

                /* Home Phone */
                new Field(FieldType.Combined, AddressRepository.Columns.HomePhone.ToLower(), f =>
                {
                    f.Required = true;
                    f.Fields = new Field[2];

                    List<string> options = UISelectOptions[f.CodeName + "-label"];
                    /* Prefix */
                    new Field(FieldType.Select, null, a =>
                    {
                        a.SelectOptions = options.ToArray();
                        a.SelectValues = UISelectValues[f.CodeName + "-label"].ToArray();
                        a.Value = edit ? ((int)SelectedEntity.HomePhonePrefix).ToString("0#") : String.Empty;
                        f.Fields[0] = a;
                    }, null);

                    /* Phone */
                    new Field(FieldType.Number, null, b =>
                    {
                        b.Size = 10;
                        b.Value = edit ? SelectedEntity.HomePhone.ToString() : String.Empty;
                        f.Fields[1] = b;
                    }, null);

                    g.Fields[9] = f;
                }, fs, false);

                #endregion

            }, fs);
            #endregion

            return fs.JSON();
        }
    }
}
